package com.assembler.app;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.assembler.app.debruij.DeBruijnEdgeFactory;
import com.assembler.app.debruij.DeBruijnGraph;

/**
 * This class provides implementation to construct the De Bruijn Graph.
 * 
 * This class is a part of the lab rotation project for PhD  students in January 2014.
 * School of Computing
 * National University Singapore.
 * 
 * @author Iana Pyrogova, Abha Belorkar
 * @version 1.0.1
 */
public class GenomAssembler {

	/**
	 * Extract k-mers from all reads.
	 * 
	 * @param reads
	 * @param k
	 * @return
	 */
	public static List<String> buildKMers(String read, int k) {
		List<String> kmers = new LinkedList<String>();

		for (int s = 0, e = k; e <= read.length(); ++s, ++e) {
			kmers.add(read.substring(s, e));
		}

		//Collections.shuffle(kmers);
		return kmers;
	}

	/**
	 * print all k-mers
	 */
	public static void printAllKmers(Collection<String> str) {
		for (String k : str) {
			System.out.println(k);
		}
	}

	/**
	 * Construct De Bruijn Graph.
	 */
	public static DeBruijnGraph getDeBruijnGraph(Collection<String> kmers,
			boolean allowRepeatedEdges) {
		DeBruijnGraph graph = new DeBruijnGraph(new DeBruijnEdgeFactory());
		for (String kmer : kmers) {
			int s = 0;
			int e = kmer.length();

			String sourceVertex = kmer.substring(s, e - 1);
			String targetVertex = kmer.substring(s + 1, e);
			if (!graph.containsVertex(sourceVertex)) {
				graph.addVertex(sourceVertex);
			}
			if (!graph.containsVertex(targetVertex)) {
				graph.addVertex(targetVertex);
			}
			if (!graph.containsEdge(sourceVertex, targetVertex) || allowRepeatedEdges) {
				graph.addEdge(sourceVertex, targetVertex);
			}
		}
		return graph;

	}
}
